// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_main");
    var settlementOverdueDetail_css = require("./settlementOverdueDetail.css");
    var html_charge_day = require("./htmlChargeDay.html");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var databind = require("databind");
    var firstVisited = 0;
    var tools = require("tools");
    var auditState;
    var leftM;
    var ym;
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(page){
    	leftM = auditState = base.getUrlParam("leftM");
    	ym = page.params.ym;
    	leftM = leftM == null?4:leftM;
    	footer.init();  	
    	$("#footer").removeClass("none");

    	leftMenu.init(leftM);
    	
    	$(".serviceFeeIncome").parents("li.leftMenu-item").addClass("leftMenu-open");
    	$(".leftMenu-item").removeClass("leftMenu-active");
    	$(".settlementOverdue").addClass("leftMenu-active");
    	
    	topMenu.init(2);
    	$(".footer").removeClass("none");
    	
    	inquiryInfo();
		initEvent();
		router.init();
		
		if(firstVisited != 1){
			$(".table-section").removeClass("none");
		}
    };
    
	var initEvent = function(){
		// 详情
		$("#settlementOverdueDetail").on("click", ".table_details_link1", function() {
			var ym = $(this).parent("tr.tr_style").attr("yearMonth");
			var ii = layer.open({
			  title: ' ',
			  type: 1,
			  skin: 'layui-layer-rim', //加上边框
			  area: ['750px', '900px'], //宽高
			  content: html_charge_day,
			  btn: ['', ''],
			  yes: function(){
			  	
			  }
			});
			
			// 弹出框样式
			var $chargeDayContentPar = $(".chargeDayContent").parent();
			$chargeDayContentPar.siblings("div.layui-layer-btn").remove();
			$chargeDayContentPar.siblings("div.layui-layer-title").css("background-color", "#0099FA");
			
			// 显示年月日期
			var yearCurrent = parseFloat(ym.substring(0, ym.indexOf("-")));
	    	var monthCurrent = parseFloat(ym.substring(ym.indexOf("-")+1, ym.length));
	    	var monthBackward;
	    	var monthForward;
	    	var yearMonthCenter;
	    	if(monthCurrent == 1) {
	    		monthBackward = 12;
	    		monthForward = 2;
	    	}else if(monthCurrent == 12) {
	    		monthBackward = 11;
	    		monthForward = 1;
	    	}else if(monthCurrent > 1 && monthCurrent < 12) {
	    		monthBackward = monthCurrent -1;
	    		monthForward = monthCurrent + 1;
	    	}
	    	if(monthCurrent < 10) {
	    		monthCurrent = "0" + monthCurrent;
	    	}
	    	if(monthBackward < 10) {
	    		monthBackward = "0" + monthBackward;
	    	}
	    	if(monthForward < 10) {
	    		monthForward = "0" + monthForward;
	    	}
	    	$(".chargeDayContent .center").html(yearCurrent + "年" + monthCurrent + "月");
	    	$(".chargeDayContent .backMonth").html(monthBackward + "月");
	    	$(".chargeDayContent .forwMonth").html(monthForward + "月");
	    	
	    	// 月份向前或向后
	    	// 向前
			$(".chargeDayContent").on("click", ".forward", function() {
				forwardOrBackward(true, true);
			})
			
			// 向后
			$(".chargeDayContent").on("click", ".backward", function() {
				forwardOrBackward(false, true);
			})
			
		});
		
		// 向前
		$("#settlementOverdueDetail").on("click", ".forward", function() {
			forwardOrBackward(true, false);
		})
		
		// 向后
		$("#settlementOverdueDetail").on("click", ".backward", function() {
			forwardOrBackward(false, false);
		})
    };
    
    var inquiryInfo = function() {
    	var yearCurrent = parseFloat(ym.substring(0, ym.indexOf("-")));
    	var monthCurrent = parseFloat(ym.substring(ym.indexOf("-")+1, ym.length));
    	var monthBackward;
    	var monthForward;
    	var yearMonthCenter;
    	if(monthCurrent == 1) {
    		monthBackward = 12;
    		monthForward = 2;
    	}else if(monthCurrent == 12) {
    		monthBackward = 11;
    		monthForward = 1;
    	}else if(monthCurrent > 1 && monthCurrent < 12) {
    		monthBackward = monthCurrent -1;
    		monthForward = monthCurrent + 1;
    	}
    	if(monthCurrent < 10) {
    		monthCurrent = "0" + monthCurrent;
    	}
    	if(monthBackward < 10) {
    		monthBackward = "0" + monthBackward;
    	}
    	if(monthForward < 10) {
    		monthForward = "0" + monthForward;
    	}
    	$("#settlementOverdueDetail .center").html(yearCurrent + "年" + monthCurrent + "月");
    	$("#settlementOverdueDetail .backMonth").html(monthBackward + "月");
    	$("#settlementOverdueDetail .forwMonth").html(monthForward + "月");
    	
    	/*base.fnAjax({
			action: ".json",
			async: false,
			nocode: true,
			oData: {
				pageIndex: pageIndex,
                pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					
				}else {
					//
				}
			}
		})*/
    };
    
    //月份向前或向后
    var forwardOrBackward = function(forward, isChargeDay) {
    	var yearLater;
    	var monthLater;
    	var yearMonthLater;
    	var monthBackward;
    	var monthForward;
    	var yearMonthCurrent;
    	if(isChargeDay == false) { //非弹出框
    		yearMonthCurrent = $("#settlementOverdueDetail .center").text();
    	}else if (isChargeDay == true) { //是弹出框
    		yearMonthCurrent = $(".chargeDayContent .center").text();
    	}
    	
    	var year = parseFloat(yearMonthCurrent.substr(0, 4));
    	var month = parseFloat(yearMonthCurrent.substr(yearMonthCurrent.indexOf("年") + 1, 2));
    	// 1. 计算中间年月
    	if(forward == true) { //向前
    		month = month + 1;
    		if(month > 0 && month < 10) {
    			monthLater = month;
    			yearLater = year;
    		}else if(month >= 10 && month <= 12) {
    			monthLater = month;
    			yearLater = year;
    		}else if(month > 12) {
    			monthLater = 1;
    			yearLater = year + 1;
    		}
    	}else { // 向后
    		month = month - 1;
    		if(month <= 0) {
    			monthLater = 12;
    			yearLater = year - 1;
    		}else if(month > 0 && month < 10) {
    			monthLater = month;
    			yearLater = year;
    		}else if(month >= 10 && month <= 12) {
    			monthLater = month;
    			yearLater = year;
    		}else if(month > 12) {
    			monthLater = 1;
    			yearLater = year - 1;
    		}
    	}
    	// 2. 计算前后月份
    	if(monthLater == 1) {
    		monthBackward = 12;
    		monthForward = 2;
    	}else if(monthLater == 12) {
    		monthBackward = 11;
    		monthForward = 1;
    	}else if(monthLater > 1 && monthLater < 12) {
    		monthBackward = monthLater -1;
    		monthForward = monthLater + 1;
    	}
    	// 3. 月份前加"0"
    	if(monthLater < 10) {
    		monthLater = "0" + monthLater;
    	}
    	if(monthBackward < 10) {
    		monthBackward = "0" + monthBackward;
    	}
    	if(monthForward < 10) {
    		monthForward = "0" + monthForward;
    	}
    	if(isChargeDay == false) {
    		$("#settlementOverdueDetail .center").html(yearLater + "年" + monthLater + "月");
			$("#settlementOverdueDetail .backMonth").html(monthBackward + "月");
			$("#settlementOverdueDetail .forwMonth").html(monthForward + "月");
    	}else if (isChargeDay == true) {
    		$(".chargeDayContent .center").html(yearLater + "年" + monthLater + "月");
	    	$(".chargeDayContent .backMonth").html(monthBackward + "月");
	    	$(".chargeDayContent .forwMonth").html(monthForward + "月");
    	}
    };
    
    return {
        init:init
    };
});